﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TuongLai_Schools
{
    public partial class frmHengoaingu : Form
    {
        public frmHengoaingu()
        {
            InitializeComponent();
        }
        cls_DM_Hengoaingu objHengoaingu = new cls_DM_Hengoaingu();
        private void frmHengoaingu_Load(object sender, EventArgs e)
        {
            GetAll_Hengoaingu();
        }
        private void GetAll_Hengoaingu()
        {
            gridControl1.DataSource = objHengoaingu.GetAll_Hangtruong();
        }
        private void Save_Hengoaingu()
        {
            clsDM_Hengoaingu objdmhnn = new clsDM_Hengoaingu();
            objdmhnn.TenHeNgoaiNgu = gridView1.GetFocusedRowCellValue("TenHeNgoaiNgu").ToString();
            objdmhnn.GhiChu = gridView1.GetFocusedRowCellValue("GhiChu").ToString();
            bool blnResult = false;
            if (ucDataButton1.DataMode == DataState.Insert)
            {
                objdmhnn.HeNgoaiNguID = 1;
                objdmhnn.isChangeable = false;
                blnResult = objHengoaingu.Insert(objdmhnn);
            }
            else
            {
                objdmhnn.HeNgoaiNguID = (int)gridView1.GetFocusedRowCellValue("HeNgoaiNguID");
                objdmhnn.isChangeable = (bool)gridView1.GetFocusedRowCellValue("isChangeable");
                blnResult = objHengoaingu.update(objdmhnn);
            }

            if (blnResult)
            {
                clsCommon.SaveSuccessfully();
                ucDataButton1.DataMode = DataState.View;
                gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
                gridView1.OptionsBehavior.Editable = false;
            }
            else
            {
                clsCommon.SaveNotSuccessfully();
                return;
            }
        }

        private void ucDataButton1_InsertHandler()
        {
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            gridView1.OptionsBehavior.Editable = true;
        }

        private void ucDataButton1_DeleteHandler()
        {
            if (clsCommon.ConfirmDeletion(gridView1.GetFocusedRowCellValue("TenHeNgoaiNgu").ToString()) == DialogResult.No)
                return;
            objHengoaingu.delete((int)gridView1.GetFocusedRowCellValue("HeNgoaiNguID"));
            GetAll_Hengoaingu();
        }

        private void ucDataButton1_SaveHandler()
        {
            Save_Hengoaingu();
        }

        private void ucDataButton1_CancelHandler()
        {
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            gridView1.OptionsBehavior.Editable = false;
            GetAll_Hengoaingu();
        }

        private void ucDataButton1_EditHandler()
        {
            gridView1.OptionsBehavior.Editable = true;
        }

    }
}
